(function() {
	var $wrapper = $("div.table-wrapper");
	var $content = $wrapper.closest('.ui-tabs-panel');
	
	var $keyword = $("input.keyword");
	$keyword.css('color', '#666');
	$keyword.focus(function() {
		if ($(this).val() == '关键字') {
			$(this).css('color', 'black');
			$(this).val('');
		}
	});
	$keyword.blur(function() {
		if ($(this).val() == '') {
			$(this).val('关键字');
			$(this).css('color', '#666');
		}
	});
	
	function getFilter()
	{
		var filter = {};
		var $form = $("form.filter");
		filter.type = $("select[name='type']", $form).val();
		var cids = null;
		var c = $("select[name='sellerCids']", $form);
		var cid = c.val();
		if (cid)
		{
			cids = [cid];
			$("option[parent='" + cid + "']", $form).each(function(){
				cids.push($(this).val());
			});
		}
		if (cids)
		{
			filter.sellerCids = cids.join();
		}
		var keyword = $("input[name='keyword']", $form).val();
		if (keyword == '关键字')
		{
			keyword = null;
		}
		if (keyword)
		{
			filter.keyword = keyword;
		}
		return filter;
	}
	
	$keyword.keypress(function(e){
		if (e.which === 13)
		{
			var filter = getFilter();
			$wrapper.data('filter', filter);
			loadPage(1);
			return false;
		}
	});
	
	$("button.search", $content).click(function(){
		var filter = getFilter();
		$wrapper.data('filter', filter);
		loadPage(1);
		return false;
	});
	
	$("#list-dialog").dialog({
		autoOpen: false,
		modal: true,
		width: 400
	});
	
	function getSelection()
	{
		var selectedItems = [];
		var $table = $('table', $wrapper);
		var selectedRows = $('tr.row-selected', $table);
		selectedRows.each(function(){
			selectedItems.push($(this).attr('num_iid'));
		});
		return selectedItems;
	}

	$('button.batch-change', $content).click(function(){
		var items = getSelection();
		if (!items || items.length == 0)
		{
			alert('未选中项目。');
			return false;
		}
		var $dialog = $("#list-dialog");
		$dialog.dialog("option", "buttons", {
			确定: function() {
				var dayOfWeek = $('form select', $dialog).val();
				var time = $('form input[name="time"]', $dialog).val();
				$.ajax({
					url: 'schedule-listing',
					data: {numIids: items.join(), dayOfWeek: dayOfWeek, time: time},
					type: 'POST',
					success: function(data, textStatus, jqXHR){
						if (window.common.isOverflow(jqXHR))
						{
							return false;
						}
						$dialog.dialog('close');
						reload();
					}
				});
				return false;
			},
			取消: function() {
				$dialog.dialog('close');
				return false;
			}
		});
		$dialog.dialog("open");
		return false;
	});
	
	$('button.batch-cancel', $content).click(function(){
		var items = getSelection();
		if (!items || items.length == 0)
		{
			alert('未选中项目。');
			return false;
		}
		window.loading.show($wrapper);
		$.ajax({
			url: 'cancel-job',
			data: {numIids: items.join()},
			type: 'POST',
			success: function(data) {
				reload();
			},
			error: function(jqXHR, textStatus, errorThrown) {
				alert(textStatus);
				var headers = jqXHR.getAllResponseHeaders();
				alert(headers);
				alert(errorThrown);
			}
		});
	});
	
	function loadPage(number, callback) {
		var $table = $('table', $wrapper);
        var limit = parseInt($table.attr('pageSize'));
        var offset = (number - 1) * limit;
        var filter = $wrapper.data('filter') || {};
        if (!isNaN(limit))
        {
        	filter = $.extend(filter, {'option.limit': limit, 'option.offset': offset});
        }
        var url = "inventory-items";
        window.loading.show($wrapper);
		$.ajax({
			url: url,
			data: filter,
			type: 'POST',
			success: function(data) {
				$wrapper.html(data);
				$table = $('table', $wrapper);
				var pageCount = parseInt($table.attr("pages"));
				$(".pager", $content).pager({ 
					pagenumber: number, 
					pagecount: pageCount, 
					buttonClickCallback: loadPage, 
					firstLabel: "首页", 
					prevLabel: "前一页", 
					nextLabel: "下一页", 
					lastLabel: "末页" 
				});
				if (callback)
				{
					callback();
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
				alert(textStatus);
				var headers = jqXHR.getAllResponseHeaders();
				alert(headers);
				alert(errorThrown);
			}
		});
    }
	
	function reload(callback)
	{
		var $table = $('table', $wrapper);
		var i = parseInt($table.attr("pageIndex"));
		loadPage(i+1, callback);
	}
	
	window.loading.show($wrapper);
	$.ajax({
		url: 'inventory-items',
		type: 'POST',
		success: function(data) {
			$wrapper.html(data);
			var $table = $('table', $wrapper);
			$(".pager", $content).pager({ 
				pagenumber: 1, 
				pagecount: parseInt($table.attr('pages')), 
				buttonClickCallback: loadPage, 
				firstLabel: "首页", 
				prevLabel: "前一页", 
				nextLabel: "下一页", 
				lastLabel: "末页" 
			});
		},
		error: function(jqXHR, textStatus, errorThrown) {
			alert(textStatus);
			var headers = jqXHR.getAllResponseHeaders();
			alert(headers);
			alert(errorThrown);
		}
	});
	
	window.listInventory = {};
	window.listInventory.reload = reload;
})();
